Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: sha512_database_verify.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script allows users to verify SHA-512 hashes against a database.
- - It loads a SHA-512 dictionary from a file, prompts the user to input a string & computes the SHA-512 hash of the input string.
- - It then checks if the user input string already exists in the dictionary.
- - If the string is not found, the user has the option to save it to the database.
- Functions:
- - sha512sum(string):
- Returns the SHA-512 hash of a given string.
- - load_sha512_database(filename):
- Loads the SHA-512 dictionary from a file.
- - save_sha512_database(sha512_dict, filename):
- Saves the SHA-512 dictionary to a file.
- - main():
- Main function to execute the script.
- Requirements:
- - Python 3.x
- Usage:
- - Run the script and follow the prompts. Enter a string to look up its SHA-512 hash, or type 'exit' to quit.
- - If the string is not found in the database, you will be prompted to save it.
- Additional Notes:
- - Make sure the database file exists and is accessible.
- - The database file will be overwritten if any changes are made.
- """
- import hashlib
- def sha512sum(string):
- """
- Return the SHA-512 hash of a given string.
- Args:
- string (str): The input string for which the SHA-512 hash will be calculated.
- Returns:
- str: The SHA-512 hash of the input string.
- """
- return hashlib.sha512(string.encode()).hexdigest()
- def load_sha512_database(filename):
- """
- Load the SHA-512 dictionary from a file.
- Args:
- filename (str): The filename of the file containing the SHA-512 dictionary.
- Returns:
- dict: The loaded SHA-512 dictionary.
- """
- sha512_dict = {}
- with open(filename, 'r', encoding='utf-8') as infile:
- for line in infile:
- # Extract string and SHA-512 hash from each line
- parts = line.strip().split(':', 1)
- if len(parts) == 2:
- string, sha512_hash = parts
- # Remove quotes and whitespace from string
- string = string.strip().strip('"')
- # Remove quotes, whitespace, and trailing comma from SHA-512 hash
- sha512_hash = sha512_hash.strip().strip('"').strip(',')
- # Add to the dictionary
- sha512_dict[string] = sha512_hash
- return sha512_dict
- def save_sha512_database(sha512_dict, filename):
- """
- Save the SHA-512 dictionary to a file.
- Args:
- sha512_dict (dict): The SHA-512 dictionary to be saved.
- filename (str): The filename of the file where the SHA-512 dictionary will be saved.
- """
- with open(filename, 'w', encoding='utf-8') as outfile:
- for string, sha512_hash in sorted(sha512_dict.items()):
- outfile.write(f'"{string}": "{sha512_hash}",\n')
- def main():
- # Load the SHA-512 dictionary
- database_filename = 'sum_sha512_dict.txt'
- sha512_dict = load_sha512_database(database_filename)
- while True:
- # Get user input
- user_input = input("\nEnter a string to look up its SHA-512 hash (or type 'exit' to quit): ").strip()
- # Check if the user wants to exit
- if user_input.lower() == 'exit':
- print("\nExiting the program...\tGoodbye!\n")
- break
- # Compute the SHA-512 hash of the user input
- user_sha512 = sha512sum(user_input)
- print("\nComputed SHA-512 hash:")
- print(user_sha512)
- # Check if the SHA-512 hash exists in the dictionary
- if user_input in sha512_dict:
- print(f"\nThe user string '{user_input}' was found in the database.")
- print(f"\nUser string: {user_input}")
- print(f"SHA-512 hash: {sha512_dict[user_input]}")
- else:
- print(f"\nThe user string '{user_input}' was not found in the database.")
- # Ask the user if they want to save the string and its SHA-512 hash to the database
- save_option = input("\nDo you want to save this string and its SHA-512 hash to the database?\n1: Yes\n2: No\n\nMake your selection (1 or 2): ").strip()
- if save_option == '1':
- sha512_dict[user_input] = user_sha512
- save_sha512_database(sha512_dict, database_filename)
- print("\nString and its SHA-512 hash saved to the database.")
- else:
- print("\nString and its SHA-512 hash not saved to the database!")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement